Skip to content

gh-156953: Fix a reference leak in curses window.insnstr() - #156954

Merged
serhiy-storchaka merged 1 commit into
python:mainfrom
fedonman:fix-curses-insnstr-bytes-refleak
Sep 7, 2026
Merged

serhiy-storchaka merged 1 commit into
python:mainfrom
fedonman:fix-curses-insnstr-bytes-refleak

Conversation

@fedonman

@fedonman fedonman commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

_curses_window_insnstr_impl() frees the wide string when curses_wattr_save() or curses_wattrset() fails, but never decrefs the bytes object PyCurses_ConvertToString() returned as a new reference, so each failing call leaks one reference to the caller's argument. GH-145609 added that decref to addstr(), addnstr() and insstr() and skipped insnstr(); this adds the missing line and a test covering all four.

There is no NEWS entry because no released version can reach the leak: 3.15 has the same omission, but detaching a window arrived with the multi-terminal screen API in GH-151748, so nothing there makes the rendition call fail.

insnstr() sets the window rendition before the write.  When that fails
it released the wide string but not the bytes object that
PyCurses_ConvertToString() returned as a new reference, so each failing
call leaked one reference to the caller's argument.  addstr(), addnstr()
and insstr() already release it at the same point.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

It is worth to backport the fix to 3.15, even if we cannot reproduce the leak there.

@serhiy-storchaka serhiy-storchaka added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 7, 2026
@serhiy-storchaka
serhiy-storchaka merged commit 23180c5 into python:main Sep 7, 2026
61 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @fedonman for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @fedonman and @serhiy-storchaka, I could not cleanly backport this to 3.15 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 23180c50082fe98784c78511b335d7274ed87fb7 3.15

clin1234 pushed a commit to clin1234/cpython that referenced this pull request Sep 12, 2026
…honGH-156954)

insnstr() did not release the bytes object it converted its argument to
when setting the attributes failed, unlike addstr(), addnstr() and
insstr().
@serhiy-storchaka serhiy-storchaka added needs backport to 3.15 pre-release feature fixes, bugs and security fixes and removed needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Oct 10, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @fedonman for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @fedonman and @serhiy-storchaka, I could not cleanly backport this to 3.15 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker 23180c50082fe98784c78511b335d7274ed87fb7 3.15

@bedevere-app

bedevere-app Bot commented Oct 10, 2026

Copy link
Copy Markdown

GH-159108 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Oct 10, 2026
serhiy-storchaka added a commit that referenced this pull request Oct 10, 2026
…-156954) (GH-159108)

insnstr() did not release the bytes object it converted its argument to
when setting the attributes failed, unlike addstr(), addnstr() and
insstr().
(cherry picked from commit 23180c5)

Co-authored-by: Vyron Vasileiadis <hi@fedonman.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants